import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {
	login,
	logout,
	updateuser,
	empowerphone
} from '@/api/login'
import {
	getToken,
	setToken,
	removeToken
} from '@/utils/auth'

const baseUrl = config.baseUrl

const user = {
	state: {
		token: getToken(), //token
		nickname: storage.get(constant.nickname), //用户名
		role: storage.get(constant.role), //角色
		permissions: storage.get(constant.permissions), //权限
		avatar: storage.get(constant.avatar), //头像
		phone: storage.get(constant.phone), //电话
		userid: storage.get(constant.userid), //用户id
		usersex: storage.get(constant.usersex), //用户性别
		status: storage.get(constant.status) //用户性别
	},

	mutations: {
		// token
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		// 用户名
		SET_NICKNAME: (state, nickname) => {
			state.nickname = nickname
			storage.set(constant.nickname, nickname)
		},

		// 角色
		SET_ROLE: (state, role) => {
			state.role = role
			storage.set(constant.role, role)
		},
		// 权限
		SET_PERMISSIONS: (state, permissions) => {
			state.permissions = permissions
			storage.set(constant.permissions, permissions)
		},
		// 头像
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
			storage.set(constant.avatar, avatar)
		},
		// 电话
		SET_PHONE: (state, phone) => {
			state.phone = phone
			storage.set(constant.phone, phone)
		},
		// 用户id
		SET_USERID: (state, userid) => {
			state.userid = userid
			storage.set(constant.userid, userid)
		},
		//用户性别
		SET_USERSEX: (state, usersex) => {
			state.usersex = usersex
			storage.set(constant.usersex, usersex)
		},
		//用户状态
		SET_STATUS: (state, status) => {
			console.log(status);
			state.status = status
			storage.set(constant.status, status)
		}
	},

	actions: {
		//获取电话号码存储用户信息到缓存
		empowerPhone({
			commit,
			state
		}, code) {
			return new Promise((resolve, reject) => {
				empowerphone(code).then(res => {
					commit('SET_NICKNAME', res.data.name)
					commit('SET_ROLE', res.data.role)
					commit('SET_AVATAR', res.data.avatar)
					commit('SET_PHONE', res.data.phone)
					commit('SET_USERID', res.data.id)
					commit('SET_USERSEX', res.data.sex)
					commit('SET_STATUS', res.data.reserveTwo)
					resolve(res)
				})
			})
		},
		// 修改用户信息
		updateUser({
			commit,
			state
		}, data) {
			return new Promise((resolve, reject) => {
				updateuser(data).then(res => {
					commit('SET_NICKNAME', res.data.name)
					commit('SET_ROLE', res.data.role)
					commit('SET_AVATAR', res.data.avatar)
					commit('SET_PHONE', res.data.phone)
					commit('SET_USERID', res.data.id)
					commit('SET_USERSEX', res.data.sex)
					commit('SET_STATUS', res.data.reserveTwo)
					resolve(res)
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 退出系统
		LogOut({
			commit,
			state
		}, userid) {
			return new Promise((resolve, reject) => {
				logout(userid).then(res => {
					console.log(res);
					console.log("退出");
					commit('SET_TOKEN', '')
					commit('SET_ROLE', [])
					commit('SET_PERMISSIONS', [])
					commit('SET_NICKNAME', '')
					commit('SET_AVATAR', '')
					commit('SET_PHONE', '')
					commit('SET_USERID', '')
					commit('SET_USERSEX', '')
					commit('SET_STATUS', '')
					removeToken()
					storage.clean()
					resolve(res.data)
				}).catch(error => {
					console.log(error);
					reject(error)
				})
			})
		}
	}
}

export default user
